<?php

/**
 * @name Uri
 * @author Roman Metlinskyi <r.metlinskyi@gmail.com>
 */
class Uri extends Object
{

  private $url;
  public $AbsolutePath;
  public $Port;
  public $Segments;
  public $Query;
  public $Params;

  public function __construct($url)
  {
    $this->url = $url;
    $this->__parse();
  }

  private function __parse()
  {
    $uri = explode('?', $this->url);

    // get path and query
    if (isset($uri[0])) {
      $this->AbsolutePath = $uri[0];
      $this->Segments = explode('/', rtrim(ltrim($this->AbsolutePath, '/'), '/'));
    }

    // get params
    if (isset($uri[1])) {
      $this->Query = str_replace('&amp;', '&', $uri[1]);
      foreach (explode('&', $this->Query) as $param) {
        //resolve param
        $p = explode('=', $param);
        if (!empty($p[0]))
          $this->Params[$p[0]] = $p[1];
      }
    }
  }

  public function __toString()
  {
    return $this->url;
  }

}